﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FPNN
{
    internal delegate Fix16 NeuronFunctionDelegate(Fix16 x);

    internal class Neuron
    {
        private static Fix16 SigmoidFunction(Fix16 x)
        {
            return Fix16.Create(1 / (1 + Math.Exp(-x.ToDouble())));
        }

        private static Fix16 LeaniarFunction(Fix16 x)
        {
            return x;
        }

        private Fix16[] _weights;
        private Fix16 _bias;
        private Fix16 _input;
        private UInt16 i = 0;
        private byte func;

        public Fix16[] Weights
        {
            get { return _weights; }
            set { _weights = value; }
        }

        public Fix16 Input
        {
            set { _input += value * _weights[i]; i++; }
        }

        public Fix16 Output
        {
            get
            {
                if (i == _weights.Length)
                {
                    return _neuronFunctions[func](_input + _bias);
                }
                throw new Exception("Данные по некоторым входам не получены.");
            }
        }

        private NeuronFunctionDelegate[] _neuronFunctions;

        public Neuron(Fix16[] weights, Fix16 bias, byte funcType)
        {
            _weights = weights;
            _bias = bias;
            func = funcType;

            _neuronFunctions = new NeuronFunctionDelegate[3];

            //_neuronFunctions[0] = new NeuronFunctionDelegate(Neuron.ThresholdFunction);
            _neuronFunctions[1] = new NeuronFunctionDelegate(Neuron.LeaniarFunction);
            _neuronFunctions[2] = new NeuronFunctionDelegate(Neuron.SigmoidFunction);
        }
    }
}